using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SCHD
{
    public partial class WFCat_Mant :TDV.Controles.Base.WinForm.BaseForm_Tipo01
    {
        LBCategoria CategoriaMod = new LBCategoria();
        public WFCat_Mant()
        {
            InitializeComponent();
        }

        private void WFCat_Mant_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
            txtCategoria.CharacterCasing = CharacterCasing.Upper;
            Cargar();
        }
        protected void Cargar()
        {
            dgCategoria.DataSource = null;
            LBCategoria cat = new LBCategoria();
            cat.Buscar();
            if (cat.Count > 0)
            {
                dgCategoria.DataSource = cat;
                BarraMensaje.Text = "Se encontraron ("+cat.Count+") registros";
            }
        }
        protected void Guardar()
        {
          try
            {
            EBCategoria categoria = new EBCategoria();
            if (txtCategoria.Text.Length > 0)
            {
                categoria.DescripcionCategoria = txtCategoria.Text;
                categoria.Grabar();
                txtCategoria.Text = "";
                txtCategoria.Select();
                Cargar();
            }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }   

        private void dgCategoria_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (dgCategoria.Columns[e.ColumnIndex].Name != null)
            {
                // Se toma la fila seleccionada 
                if (e.RowIndex > -1)
                {
                    DataGridViewRow row = dgCategoria.Rows[e.RowIndex];
                    string Codigo = row.Cells[1].Value.ToString();
                    string Descripcion = row.Cells[2].Value.ToString();
                    //string Valor = row.Cells[e.ColumnIndex].Value.ToString();
                    //string Restriccion = GrdAtributos.Columns[e.ColumnIndex].Name.ToString();
                    //DataGridViewCheckBoxCell cellSelecion = row.Cells[e.RowIndex] as DataGridViewCheckBoxCell;
                    EBCategoria cate = new EBCategoria();
                    cate.CodigoCategoria = Codigo;
                    cate.DescripcionCategoria = Descripcion;
                    CategoriaMod.Nuevo(cate);
                }

            }
        }

       
        private void WFCat_Mant_KeyPress(object sender, KeyPressEventArgs e)
        {
            // this.OnKeyPress(e);

            if (e.KeyChar == (char)Keys.Escape)
                //  MessageBox.Show("Ha presionado ESCAPAR");
                DialogResult = DialogResult.Cancel;
        }

        private void txtCategoria_KeyDown(object sender, KeyEventArgs e)
        {
            if (txtCategoria.Text.Length > 0)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    Guardar();
                }
            }
        }

        private void TsbSalir_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void TsbGuardar_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCategoria.Text))
            {
                foreach (EBCategoria __Categoria in CategoriaMod)
                {
                    __Categoria.Grabar();
                }
                CategoriaMod.Clear();
                DialogResult = DialogResult.OK;
            }
            else
            {
                Guardar();
            }
        }

        private void TsbEliminar_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dgCategoria.Rows)
            {
                DataGridViewCheckBoxCell ck1 = row.Cells[0] as DataGridViewCheckBoxCell;

                //CheckBox ck = (CheckBox)row.Cells[0].Value;
                if (Convert.ToBoolean(ck1.Value))
                {
                    try
                    {
                        EBCategoria cat = new EBCategoria();
                        cat.CodigoCategoria = row.Cells[1].Value.ToString();
                        cat.Eliminar();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString(), "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
            Cargar();
        }

        private void dgCategoria_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dgCategoria.IsCurrentCellDirty)
            {
                dgCategoria.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

           }
}

